{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# rust jupyter\n",
    "\n",
    "## install\n",
    "\n",
    "```\n",
    "$ cargo install --locked evcxr_jupyter\n",
    "$ evcxr_jupyter --install\n",
    "\n",
    "```\n",
    "* readme\n",
    "https://github.com/evcxr/evcxr/blob/main/evcxr_jupyter/README.md\n",
    "\n",
    "* tour\n",
    "https://github.com/evcxr/evcxr/blob/main/evcxr_jupyter/samples/evcxr_jupyter_tour.ipynb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "vscode": {
     "languageId": "rust"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello, vec1: [1, 2, 3, 4, 5]\n"
     ]
    }
   ],
   "source": [
    "let vec1 = vec![1,2,3,4,5];\n",
    "println!(\"vec1: {:?}\", vec1);\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "vscode": {
     "languageId": "rust"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Complex { re: 1.1, im: -1.2 } + Complex { re: 22.2, im: 33.3 } = Complex { re: 23.3, im: 32.099999999999994 }\n"
     ]
    }
   ],
   "source": [
    ":dep num\n",
    "use num::complex::Complex;\n",
    "\n",
    "let c1 = Complex::new(1.1, -1.2);\n",
    "let c2 = Complex::new(22.2, 33.3);\n",
    "println!(\"{:?} + {:?} = {:?}\", c1, c2, c1+c2);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "vscode": {
     "languageId": "rust"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table><tr><td>1</td><td>2</td><td>3</td></tr><tr><td>4</td><td>5</td><td>6</td></tr><tr><td>7</td><td>8</td><td>9</td></tr></table>"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "use std::fmt::Debug;\n",
    "\n",
    "pub struct Matrix<T> {\n",
    "    pub values: Vec<T>, \n",
    "    pub row_size: usize\n",
    "}\n",
    "\n",
    "impl<T: Debug> Matrix<T> {\n",
    "    pub fn evcxr_display(&self) {\n",
    "        let mut html = String::new();\n",
    "        html.push_str(\"<table>\");\n",
    "        for r in 0..(self.values.len() / self.row_size) {\n",
    "            html.push_str(\"<tr>\");\n",
    "            for c in 0..self.row_size {\n",
    "                html.push_str(\"<td>\");\n",
    "                html.push_str(&format!(\"{:?}\", self.values[r * self.row_size + c]));\n",
    "                html.push_str(\"</td>\");\n",
    "            }\n",
    "            html.push_str(\"</tr>\");\n",
    "        }\n",
    "        html.push_str(\"</table>\");\n",
    "        println!(\"EVCXR_BEGIN_CONTENT text/html\\n{}\\nEVCXR_END_CONTENT\", html);\n",
    "    }\n",
    "}\n",
    "let m = Matrix {values: vec![1,2,3,4,5,6,7,8,9], row_size: 3};\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "vscode": {
     "languageId": "rust"
    }
   },
   "outputs": [],
   "source": [
    ":dep evcxr_input\n",
    "let name = evcxr_input::get_string(\"Name?\");\n",
    "let password = evcxr_input::get_password(\"Password?\");"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## fibonaqi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "vscode": {
     "languageId": "rust"
    }
   },
   "outputs": [],
   "source": [
    "pub fn fib(x: i32) -> i32 {\n",
    "    if x <= 2 {1} else {fib(x - 2) + fib(x - 1)}\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "vscode": {
     "languageId": "rust"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(1..13).map(fib).collect::<Vec<i32>>()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "vscode": {
     "languageId": "rust"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAIAAADTED8xAAAGBklEQVR4nO3TCVIcRxBAUfn+h7aRRUgIhpleasnlvQiHBczSVZn/nx8//v3x7u3f0Mvb0v8O4M3bj9DI28Z/DODN22+gi1/rrgGa+r3rGqCjj4uuAdr5tOUaoJevK64BGnm43xqgi++WWwO08GSzNUB9z9daAxT3cqc1QGVHFloDlHVwmzVATcdXWQMUdGqPNUA1Z5dYA5RyYYM1QB3X1lcDFHF5dzVABXcWVwOkd3NrNUBu91dWAyQ2ZF81QFajllUDpDRwUzVAPmPXVAMkM3xHNUAmMxZUA6QxaTs1QA7zVlMDJDB1LzVAdLOXUgOEtmAjNUBca9ZRAwS1bBc1QEQrF1EDhLN4CzVALOtXUAMEsmX/NEAUu5ZPA4SwcfM0wH57104DbLZ95zTAThEWTgNsE2TbNMAecVZNA2wQas80wGrRlkwDLBVwwzTAOjHXSwMsEna3NMAKkRdLA0wXfKs0wFzxV0oDTJRinzTALFmWSQNMkWiTNMB4udZIAwyWboc0wEgZF0gDDJN0ezTAGHlXRwMMkHpvNMBd2ZdGA9xSYGM0wHU11kUDXFRmVzTAFZUWRQOcVmxLNMA59VZEA5xQcj80wFFVl0MDHFJ4MzTAa7XXQgO8UH4nNMAzHRZCA3yryTZogMf6rIIGeKDVHmiAz7otgQb4S8MN0AB/9By/BnjXdvYa4KfOg9cA3aeuge6MXAOtmfcbDfRl2L9ooCmT/k0DHRnzRxpox4w/0UAvBvyVBhox3Yc00IXRfkcDLZjrExqoz1Cf00BxJvqSBiozziM0UJZZHqSBmgzyOA0UZIqnaKAaIzxLA6WY3wUaqMPwrtFAESZ3mQYqMLY7NJCemd2kgdwM7D4NJGZaQ2ggK6MaRQMpmdNAGsjHkMbSQDImNJwGMjGeGTSQhtlMooEcDGYeDSRgKlNpIDojmU0DoZnHAhqI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Gvj5H7TxuQEB0M1fDQiAhv40IAB6em/gP4B1AQ2EIi+6AAAAAElFTkSuQmCC"
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ":dep image = \"0.23\"\n",
    ":dep evcxr_image = \"1.1\"\n",
    "use evcxr_image::ImageDisplay;\n",
    "\n",
    "image::ImageBuffer::from_fn(256, 256, |x, y| {\n",
    "    if (x as i32 - y as i32).abs() < 3 {\n",
    "        image::Rgb([0, 0, 255])\n",
    "    } else {\n",
    "        image::Rgb([0, 0, 0])\n",
    "    }\n",
    "})"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Rust",
   "language": "rust",
   "name": "rust"
  },
  "language_info": {
   "codemirror_mode": "rust",
   "file_extension": ".rs",
   "mimetype": "text/rust",
   "name": "Rust",
   "pygment_lexer": "rust",
   "version": ""
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
